<?php
namespace app\admin\controller;
use think\Request;
use app\common\model\Notice as NoticeModel;
use think\Db;
class Notice extends  \app\admin\controller\Base{
    protected $res = array(
        'index'   => array('css' => '2,3,4', 'js' => 'list'),
        'add'   => array('css' => '2,3,4,5,9', 'js' => 'ueditor_config,ueditor_all,ueditor_lang'),
        'details'   => array('css' => '2,3,4,5,9', 'js' => 'ueditor_config,ueditor_all,ueditor_lang'),

    );

    /**
     * 首页列表
     */
    public function index(){
        $NoticeModel = new NoticeModel();
//        $search = input('get.search/a');
        $page = input('get.page');
//        $search = $this->search($search, $page);
        $list = $NoticeModel->relData(15);
        $action = '/' . Request::instance()->controller() . '/' . Request::instance()->action();
        $this->assign(
            array(
                'action'=>$action,
                'list'=>$list,
                'page'=>$page,
//                'search' => $search,
            )
        );
        return $this->fetch();
    }

    /**
     * @return \think\response\Json
     * 删除
     */
    public function delNotice(){
        $ids = strtoarray(input('post.ids'));
        $NoticeModel = new NoticeModel();

        $where['n_id'] = array('In', $ids);
        $result = $NoticeModel->where($where)->delete();
        if ($result) {
            return $this->api_success('操作成功');
        } else {
            return $this->api_dbError();
        }
    }

    /**
     * @return mixed|\think\response\Json
     * 添加
     */
    public function add(){
        $NoticeModel = new NoticeModel();

        if(Request::instance()->isPost()){
            if($_FILES['file']['name'][0]=='') {
                return $this->api_dbError("上传文件不能为空");
            }
            $rand = date('YmdHis').rand(100000,999999);
            $exe = explode('.',$_FILES['file']['name'][0]);
            $filePath = $_FILES['file']['tmp_name'][0];
            $info = $this->qnCommon($rand,$exe[1],$filePath);
            if($info['status']!=200) {
                return $this->api_dbError("上传文件格式不正确");
            }
            $data['n_image'] =QiNiu.$info['key'];
            $data['n_title'] = input('post.banner_title');
            $data['n_content'] = input('post.detail');
            $data['n_detail_content'] = input('post.content');
            $data['n_createtime'] =date('Y-m-d H:i:s');
            $res = $NoticeModel->save($data);
            if ($res) {
                $this->Jpush('','您有新的公告通知',2, array('type'=>3));

                return $this->api_success("操作成功");
            } else {
                return $this->api_dbError("操作失败");
            }
        }
        return $this->fetch();
    }

    /**
     * 获取消息详情
     * @param $n_id
     * @return mixed
     */
    public function details($n_id){
        $data = Db::table('up_notice')->where('n_id', $n_id)->find();
        $this->assign('data', $data);
        return $this->fetch();
    }
}